﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using CSFileController.Classes;
using CSFileController.Exceptions;

namespace CSFileController.Forms
{
    public partial class Form1 : Form
    {
        private static int infoMark = 1;

        private FileOperator fileOperator;

        List<RadioButton> radioButtons=new List<RadioButton>();
        private bool radioChecking = false;
        string filePath = "";

        public Form1()
        {
            InitializeComponent();

            openFileDialog1.Title = "选择一个 .cs 文件";
            //openFileDialog1.InitialDirectory = "D:\\github_repository";
            openFileDialog1.InitialDirectory = "C:\\";
            openFileDialog1.Filter = "(*.cs)|*.cs";

            radioButtons.Add(radioButtonPrior);
            radioButtons.Add(radioButtonLatter);
            radioButtons[1].Checked = true;
            this.radioButtonLatter.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
            this.radioButtonPrior.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged);
        }

        /// <summary>
        /// 打开文件选择窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOpenFile_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.ShowDialog(this);
            filePath=openFileDialog1.FileName;
            this.textBoxPath.Text = filePath;
            this.textBoxOutFileName.Text = Path.GetFileName(filePath);
        }

        /// <summary>
        /// 确保只有一个radioButton被选择（显示原文件信息或修改后文件信息）
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void radioButton_CheckedChanged(Object sender, EventArgs e)
        {
            if (!radioChecking)
                return;
            radioChecking = true;
            foreach(RadioButton radioButton in radioButtons)
            {
                RadioButton radio=sender as RadioButton;
                if (radio.Text.Equals(radioButton.Text))
                    continue;
                radioButton.Checked ^= true;
            }
            radioChecking = false;
        }

        /// <summary>
        /// 开始执行按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonRun_Click(object sender, EventArgs e)
        {
            listBoxResult.Items.Add("info " + (infoMark++) + ": ");
            try
            {
                fileOperator = new FileOperator(filePath);

                int countLine1 = fileOperator.countLine();
                double countWord1 = fileOperator.countWord();

                if (checkBoxDeleteComment.Checked)
                    fileOperator.deleteComment();
                if (checkBoxDeleteBlankLine.Checked)
                    fileOperator.deleteBlankLine();

                int countLine2 = fileOperator.countLine();
                double countWord2 = fileOperator.countWord();

                fileOperator.write(textBoxOutFileName.Text);
                fileOperator.close();

                string info = (checkBoxDeleteComment.Checked?"删除注释，":"")
                    +(checkBoxDeleteBlankLine.Checked?"删除空行，":"")
                    +"内容已写入文件" + textBoxOutFileName.Text + "中，";
                if (radioButtons[0].Checked)
                    info += "修改前文件行数为" + countLine1 
                        + "，单词数为" + countWord1;
                if (radioButtons[1].Checked)
                    info+= "修改后文件行数为" + countLine2
                        + "，文件单词数为" + countWord2;
                listBoxResult.Items.Add(info);
                        
            }catch(IOException exp)
            {
                listBoxResult.Items.Add("IO操作失败:" +exp.Message);
                fileOperator.close();
            }
            catch (ArgumentNullException)
            {
                listBoxResult.Items.Add("未选择文件");
                fileOperator.close();
            }
            catch (FileTypeException ex)
            {
                listBoxResult.Items.Add(ex.ToString());
                fileOperator.close();
            }
            checkBoxDeleteBlankLine.Checked = false;
            checkBoxDeleteComment.Checked = false;
        }

        private void checkBoxOutFileName_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxOutFileName.Checked)
            {
                try
                {
                    textBoxOutFileName.Text = Path.GetFileName(textBoxPath.Text);
                }
                catch (IOException)
                {
                    textBoxOutFileName.Text = "文件路径异常";
                }
                textBoxOutFileName.ReadOnly = true;
            }
            else
            {
                textBoxOutFileName.Text = "";
                textBoxOutFileName.ReadOnly= false;
            }
        }
    }
}
